package com.jianbing.lakk.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.jianbing.common.core.domain.model.LoginUser;
import com.jianbing.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.lakk.domain.LakkUsercoinrecord;
import com.jianbing.lakk.service.ILakkUsercoinrecordService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 金币记录Controller
 * 
 * @author windbless
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/lakk/UserCoinRecord")
public class LakkUsercoinrecordController extends BaseController
{
    @Autowired
    private ILakkUsercoinrecordService lakkUsercoinrecordService;
     private Long getappid(LoginUser loginUser) {
        return loginUser.getUserId();
     }
    /**
     * 查询金币记录列表
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(LakkUsercoinrecord lakkUsercoinrecord)
    {
        startPage();
        lakkUsercoinrecord.setUserid(getappid(SecurityUtils.getLoginUser()));
        List<LakkUsercoinrecord> list = lakkUsercoinrecordService.selectLakkUsercoinrecordList(lakkUsercoinrecord);
//        遍历list，将每个LakkUsercoinrecord的amountofcoinsobtained加起来
        int sum = 0;
        for (LakkUsercoinrecord lakkUsercoinrecord1 : list) {
            sum += lakkUsercoinrecord1.getAmountofcoinsobtained();
        }
         TableDataInfo test = new TableDataInfo();
            test.setCode(200);
            test.setTotal(sum);
            test.setMsg("检索范围内，您的金币总数为"+sum);
            List<LakkUsercoinrecord> listnow = lakkUsercoinrecordService.selectLakkUsercoinrecordList(lakkUsercoinrecord);
                test.setRows(listnow);
            return test;

    }
      @GetMapping("/list1")
    public TableDataInfo list1(LakkUsercoinrecord lakkUsercoinrecord)
    {
        startPage();
        List<LakkUsercoinrecord> list = lakkUsercoinrecordService.selectLakkUsercoinrecordList(lakkUsercoinrecord);
        return getDataTable(list);
    }

    /**
     * 导出金币记录列表
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:export')")
    @Log(title = "金币记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LakkUsercoinrecord lakkUsercoinrecord)
    {
        List<LakkUsercoinrecord> list = lakkUsercoinrecordService.selectLakkUsercoinrecordList(lakkUsercoinrecord);
        ExcelUtil<LakkUsercoinrecord> util = new ExcelUtil<LakkUsercoinrecord>(LakkUsercoinrecord.class);
        util.exportExcel(response, list, "金币记录数据");
    }

    /**
     * 获取金币记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:query')")
    @GetMapping(value = "/{userid}")
    public AjaxResult getInfo(@PathVariable("userid") Long userid)
    {
        return success(lakkUsercoinrecordService.selectLakkUsercoinrecordByUserid(userid));
    }

    /**
     * 新增金币记录
     */
//  @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:add')")
@Log(title = "金币记录", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody LakkUsercoinrecord lakkUsercoinrecord1) {
    Long systemuserid = getappid(SecurityUtils.getLoginUser());
    lakkUsercoinrecord1.setUserid(systemuserid);
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(new Date()); // 设置为当前时间
  Date nowtime = calendar.getTime();
    lakkUsercoinrecord1.setTimeofcoinacquisition(nowtime);
    int rows = lakkUsercoinrecordService.insertLakkUsercoinrecord(lakkUsercoinrecord1);
    AjaxResult ajaxResult = new AjaxResult();
    ajaxResult.put("userid", systemuserid);
    ajaxResult.put("timeofcoinacquisition", nowtime);
    ajaxResult.put("rows", rows);
    if (rows > 0) {
        ajaxResult.success();
    } else {
        ajaxResult.error();
    }
    return ajaxResult;
}

    /**
     * 修改金币记录
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:edit')")
    @Log(title = "金币记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LakkUsercoinrecord lakkUsercoinrecord)
    {
        return toAjax(lakkUsercoinrecordService.updateLakkUsercoinrecord(lakkUsercoinrecord));
    }

    /**
     * 删除金币记录
     */
//    @PreAuthorize("@ss.hasPermi('lakk:UserCoinRecord:remove')")
    @Log(title = "金币记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userids}")
    public AjaxResult remove(@PathVariable Long[] userids)
    {
        return toAjax(lakkUsercoinrecordService.deleteLakkUsercoinrecordByUserids(userids));
    }
}
